<template>
    <div class="p-2">
        <el-card shadow="never">
            <template #header>
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="申请日期：" prop="timeDate">
                        <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                            start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                    </el-form-item>
                    <el-form-item label="审批状态：" prop="approvalStatus">
                        <el-select v-model="queryParams.approvalStatus" clearable>
                            <el-option label="未处理" value="0"></el-option>
                            <el-option label="已编辑" value="1"></el-option>
                            <el-option label="拒绝" value="3"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="规格名称：" prop="specsDetail">
                        <el-input v-model="queryParams.specsDetail" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="申请人" prop="applyName" align="center"></el-table-column>
                <el-table-column label="申请日期" prop="applyDate" align="center"></el-table-column>
                <el-table-column label="交货日期" prop="deliveryDate" align="center"></el-table-column>
                <el-table-column label="采购单号" prop="purchaseOrderCode" align="center"></el-table-column>
                <el-table-column label="效果图" align="center">
                    <template #default="scope">
                        <div class="demo-image__preview">
                            <el-image style="width: 100px; height: 100px" lazy :src="scope.row.imageUrl"
                                preview-teleported :preview-src-list="[scope.row.imageUrl]" fit="cover" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="规格名称" prop="specsDetail" align="center"></el-table-column>
                <el-table-column label="单位" prop="purchaseUnit" align="center"></el-table-column>
                <el-table-column label="单价" prop="purchasePrice" align="center"></el-table-column>
                <el-table-column label="申请数量" prop="number" align="center"></el-table-column>
                <el-table-column label="采购数量" prop="purchaseNumber" align="center"></el-table-column>
                <el-table-column label="供应商" prop="supplyFactoryName" align="center"></el-table-column>
                <el-table-column label="采购申请备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="审批" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.approvalStatus === '0'" style="color: #F76F62;">未处理</span>
                        <span v-else-if="scope.row.approvalStatus === '1'" style="color: #E6A441;">已编辑</span>
                        <span v-else-if="scope.row.approvalStatus === '2'" style="color: #73C64B;">通过</span>
                        <span v-else-if="scope.row.approvalStatus === '3'" style="color: #DF9BF9;">拒绝</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="220px">
                    <template #default="scope">
                        <el-tooltip content="编辑">
                            <el-button @click="handleEditClick(scope.row)" link :style="{ fontSize: '15px' }"
                                type="success" icon="EditPen"></el-button>
                        </el-tooltip>
                        <el-tooltip content="审批" placement="top">
                            <el-button @click="handleApprovalClick(scope.row)" link :style="{ fontSize: '15px' }"
                                type="primary" icon="Document"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除">
                            <el-button @click="deleteList(scope.row)" link :style="{ fontSize: '15px' }" type="danger"
                                icon="Delete"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <editApplicationForm :visible="editShow" :initList="editObj" :suppliersList="suppliersList" :editType="editType"
            @agree="getList" @close="editClose" @submit="getList" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { getPurchaseRequestFormListApi, deletePurchaseRequestFormApi } from '@/api/sj/purchaseManagement/applicationForm/index';
import { getSuppliersApi } from '@/api/sj/purchaseManagement/supplierManagement/index';


/** 编辑 */
import editApplicationForm from './components/editApplicationForm.vue';
import useEditApplicationForm from '@/hooks/purchaseManagement/applicationForm/useEditApplicationForm';
const { editShow, editType, editObj, handleApprovalClick, handleEditClick, editClose } = useEditApplicationForm();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        approvalStatus: '',
        specsDetail: '',
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const suppliersList = ref<any>([]);

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

/** 列表删除 */
const deleteList = (row: any) => {
    ElMessageBox.confirm('确认要将此条信息彻底删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            try {
                const { code } = await deletePurchaseRequestFormApi(row.id)
                if (code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    });
                    getList();
                }
            } catch (error) {

            }

        })
        .catch(() => { });
};

const getList = async () => {

    delete queryParams.value.timeDate;

    try {
        const { code, data: { total, records } } = await getPurchaseRequestFormListApi(queryParams.value);
        console.log(data, '123123');
        if (code === 200) {
            dataList.value = records;
            totalData.value = total;
        }

    } catch (error) {

    }
};


/** 获取全部供应商列表 */
const getSuppliers = async () => {
    const { code, data } = await getSuppliersApi('1');
    if (code === 200) {
        suppliersList.value = data;
    }
};

onMounted(() => {
    getList();
    getSuppliers();
});

</script>
<style lang="scss" scoped>
.demo-image__error .image-slot {
    font-size: 30px;
}

.demo-image__error .image-slot .el-icon {
    font-size: 30px;
}

.demo-image__error .el-image {
    width: 100%;
    height: 200px;
}
</style>